package ca.cbc.msb.console.transaction;

import java.util.List;

import javax.inject.Inject;
import javax.inject.Named;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.transaction.annotation.Transactional;

import ca.cbc.medialib.dao.MediaDAO;
import ca.cbc.medialib.entity.Media;

@Named
public class MediaTransaction {
	protected Logger logger = LoggerFactory.getLogger(this.getClass());
	@Inject
	private MediaDAO mediaDAO;

	@Transactional
	public List<Media> list() {
		return mediaDAO.list();
	}
	
	@Transactional
    public boolean remove(long releaseId) {
		Media media = mediaDAO.findById(releaseId);
		if(media == null) {
			return false;	//media has been deleted by msb
		}
		
    	mediaDAO.delete(media);
    	return true;
    }

	
	/**
	 * This function load all the medias and sponsors assigned to this category
	 * @param id
	 * @return 
	 */
	@Transactional
	public Media load(long releaseId) {
		Media media = mediaDAO.findById(releaseId);
		if(media != null) {
			media.getCategories().size();
		}
		return media;
	}
}
